﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
namespace WCF.Common.DAL
{
    public class LogObjectDAL
    {
        private LoggerDBDataContext dataContext;

        private Table<LogObject> Items;
        public LogObjectDAL()
        {
            dataContext = new LoggerDBDataContext();
            Items = dataContext.LogObject;
        }

        /// <summary>获取表数据</summary>
        /// <returns>List</returns>
        public List<LogObject> Gets()
        {
            return Items.ToList();
        }
        /// <summary>根据用户编号查询该用户的全部日志</summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public List<LogObject> GetLogsByUserCode(string user)
        {
            return Items.Where(p => p.UserID == user).ToList();
        }
        /// <summary>根据时间段查询日志</summary>
        /// <param name="begin">起始时间</param>
        /// <param name="end">终止时间</param>
        /// <returns></returns>
        public List<LogObject> GetLogsByDate(DateTime begin, DateTime end)
        {
            return Items.Where(p => p.Date > begin && p.Date < end).ToList();
        }
        /// <summary>批量增加数据</summary>
        /// <param name="obj">需要处理的数据</param>
        /// <returns>返回操作结果，若操作不成功则返回错误描述信息。</returns>
        public OperateResult Insert(LogObject obj)
        {
            try
            {
                dataContext.LogObject.InsertOnSubmit(obj);
                dataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                return new OperateResult() { IsSuccess = false, Message = ex.Message };
            }
            return new OperateResult() { IsSuccess = true };
        }
        /// <summary>批量删除数据</summary>
        /// <param name="obj">需要处理的数据</param>
        /// <returns>返回操作结果，若操作不成功则返回错误描述信息。</returns>
        public OperateResult Delete(LogObject obj)
        {
            try
            {
                dataContext.LogObject.DeleteOnSubmit(obj);
                dataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                return new OperateResult() { IsSuccess = false, Message = ex.Message };
            }
            return new OperateResult() { IsSuccess = true };
        }
    }
}